package cn.sany.cterminal.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.sany.cterminal.common.constants.CterminalConst;
import cn.sany.cterminal.dao.*;
import cn.sany.cterminal.entity.*;
import cn.sany.cterminal.entity.request.StatisticQueryParam;
import cn.sany.cterminal.service.StatisticService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @Project : sany
 * @Package : cn.sany.cterminal.service
 * @Class : StatisticService
 * @Description : 统计信息service
 * @author : zhouxing
 * @CreateDate : 2020-09-14 09:54:00
 * @version : V0.5
 * @Copyright : 2020 sany.com Inc. All rights reserved.
 * @Reviewed :
 * @UpateLog :    Name    Date    Reason/Contents
 *             ---------------------------------------
 *                ****    ****    ****
 *
 */
@Service
@Slf4j
public class StatisticServiceImpl implements StatisticService {
    @Autowired(required = false)
    StatisticHistoryDao statisticHistoryDao;
    @Autowired(required = false)
    VehicleInfoDao vehicleInfoDao;
    @Autowired(required = false)
    RealtimeInfoDao realtimeInfoDao;
    /**
     *
     * @Method : getAllForkliftInfo
     * @Description :获取累计电耗信息接口
     * @return : CommonReturn
     * @author : zhouxing
     * @CreateDate : 2020-09-14 09:55:21
     *
     */
    @Override
    public List getElectricityConsum(StatisticQueryParam statisticQueryParam) {
        //createQueryParam(statisticQueryParam);
        List<VehicleInfo> vehicleList = vehicleInfoDao.selectVehicleByEnableState(CterminalConst.EnableState.ABLE);
        List<StatisticHistory> startInfoList = statisticHistoryDao.getCurrentDayList(statisticQueryParam.getStartTime());
        Map<Integer, StatisticHistory> startListMap = startInfoList.stream().collect(Collectors.toMap(StatisticHistory::getVehicleId, StatisticHistory -> StatisticHistory));
        List<ElectricityConsumVo> result = new ArrayList();
        if(DateUtil.isSameDay(statisticQueryParam.getEndTime(),new Date())){
            List<RealtimeInfo> endInfoList =  realtimeInfoDao.selectAll();
            log.info("获取车辆数据：{}条，起始数据：{}条，结束数据结束：{}条",vehicleList.size(), startInfoList.size(),endInfoList.size());
            Map<Integer, RealtimeInfo> endListMap = endInfoList.stream().collect(Collectors.toMap(RealtimeInfo::getVehicleId, RealtimeInfo -> RealtimeInfo));
            for (VehicleInfo vehicleInfo : vehicleList) {
                StatisticHistory startInfo = startInfoList.get(vehicleInfo.getId());
                RealtimeInfo endInfo = endListMap.get(vehicleInfo.getId());
                ElectricityConsumVo electricityConsumVo = new ElectricityConsumVo();
                electricityConsumVo.setDeviceId(vehicleInfo.getVinNumber());
                if(startInfo == null || endInfo == null ){
                    electricityConsumVo.setElectricityConsum("0.0");
                }else {
                    electricityConsumVo.setElectricityConsum(String.format("%.2f",endInfo.getElectricityConsume() - startInfo.getElectricityConsume()));
                }
                result.add(electricityConsumVo);
            }
        }else{
            List<StatisticHistory> endInfoList = statisticHistoryDao.getCurrentDayList(statisticQueryParam.getStartTime());
            log.info("获取车辆数据：{}条，起始数据：{}条，结束数据结束：{}条",vehicleList.size(), startInfoList.size(),endInfoList.size());
            for (VehicleInfo vehicleInfo : vehicleList) {
                StatisticHistory startInfo = startInfoList.get(vehicleInfo.getId());
                StatisticHistory endInfo = endInfoList.get(vehicleInfo.getId());
                ElectricityConsumVo electricityConsumVo = new ElectricityConsumVo();
                electricityConsumVo.setDeviceId(vehicleInfo.getVinNumber());
                if(startInfo == null || endInfo == null ){
                    electricityConsumVo.setElectricityConsum("0.0");
                }else {
                    electricityConsumVo.setElectricityConsum(String.format("%.2f",endInfo.getElectricityConsume() - startInfo.getElectricityConsume()));
                }
                result.add(electricityConsumVo);
            }
        }
        return result;
    }
    /**
     *
     * @Method : getWorkTime
     * @Description : 获取工作时长接口
     * @param statisticQueryParam :
     * @return : java.util.List
     * @author : zhouxing
     * @CreateDate : 2020-09-14 11:46:51
     *
     */
    @Override
    public List getWorkTime(StatisticQueryParam statisticQueryParam) {
        //createQueryParam(statisticQueryParam);
        List<VehicleInfo> vehicleList = vehicleInfoDao.selectVehicleByEnableState(CterminalConst.EnableState.ABLE);
        List<StatisticHistory> startInfoList = statisticHistoryDao.getCurrentDayList(statisticQueryParam.getStartTime());
        Map<Integer, StatisticHistory> startListMap = startInfoList.stream().collect(Collectors.toMap(StatisticHistory::getVehicleId, StatisticHistory -> StatisticHistory));
        List<WorkTimeVo> result = new ArrayList();
        if(DateUtil.isSameDay(statisticQueryParam.getEndTime(),new Date())){
            List<RealtimeInfo> endInfoList =  realtimeInfoDao.selectAll();
            log.info("获取车辆数据：{}条，起始数据：{}条，结束数据结束：{}条",vehicleList.size(), startInfoList.size(),endInfoList.size());
            Map<Integer, RealtimeInfo> endListMap = endInfoList.stream().collect(Collectors.toMap(RealtimeInfo::getVehicleId, RealtimeInfo -> RealtimeInfo));
            for (VehicleInfo vehicleInfo : vehicleList) {
                StatisticHistory startInfo = startInfoList.get(vehicleInfo.getId());
                RealtimeInfo endInfo = endInfoList.get(vehicleInfo.getId());
                WorkTimeVo workTimeVo = new WorkTimeVo();
                workTimeVo.setDeviceId(vehicleInfo.getVinNumber());
                if(startInfo == null || endInfo == null ){
                    workTimeVo.setWorkTime("0.0");
                }else {
                    workTimeVo.setWorkTime(String.format("%.2f",(endInfo.getBootTime() - startInfo.getBootTime())/1000/60/60));
                }
                result.add(workTimeVo);
            }
        }else{
            List<StatisticHistory> endInfoList = statisticHistoryDao.getCurrentDayList(statisticQueryParam.getStartTime());
            log.info("获取车辆数据：{}条，起始数据：{}条，结束数据结束：{}条",vehicleList.size(), startInfoList.size(),endInfoList.size());
            for (VehicleInfo vehicleInfo : vehicleList) {
                StatisticHistory startInfo = startInfoList.get(vehicleInfo.getId());
                StatisticHistory endInfo = endInfoList.get(vehicleInfo.getId());
                WorkTimeVo workTimeVo = new WorkTimeVo();
                workTimeVo.setDeviceId(vehicleInfo.getVinNumber());
                if(startInfo == null || endInfo == null ){
                    workTimeVo.setWorkTime("0.0");
                }else {
                    workTimeVo.setWorkTime(String.format("%.2f",(endInfo.getBootTime() - startInfo.getBootTime())/1000/60/60));
                }
                result.add(workTimeVo);
            }
        }
        return result;
    }
    /**
     *
     * @Method : getDistance
     * @Description : 获取累计里程接口
     * @param statisticQueryParam :
     * @return : java.util.List
     * @author : zhouxing
     * @CreateDate : 2020-09-14 12:00:01
     *
     */
    @Override
    public List getDistance(StatisticQueryParam statisticQueryParam) {
       // createQueryParam(statisticQueryParam);
        List<VehicleInfo> vehicleList = vehicleInfoDao.selectVehicleByEnableState(CterminalConst.EnableState.ABLE);
        List<StatisticHistory> startInfoList = statisticHistoryDao.getCurrentDayList(statisticQueryParam.getStartTime());
        Map<Integer, StatisticHistory> startListMap = startInfoList.stream().collect(Collectors.toMap(StatisticHistory::getVehicleId, StatisticHistory -> StatisticHistory));
        List result = new ArrayList();
        if(DateUtil.isSameDay(statisticQueryParam.getEndTime(),new Date())){
            List<RealtimeInfo> endInfoList =  realtimeInfoDao.selectAll();
            log.info("获取车辆数据：{}条，起始数据：{}条，结束数据结束：{}条",vehicleList.size(), startInfoList.size(),endInfoList.size());
            Map<Integer, RealtimeInfo> endListMap = endInfoList.stream().collect(Collectors.toMap(RealtimeInfo::getVehicleId, RealtimeInfo -> RealtimeInfo));
            for (VehicleInfo vehicleInfo : vehicleList) {
                StatisticHistory startInfo = startInfoList.get(vehicleInfo.getId());
                RealtimeInfo endInfo = endInfoList.get(vehicleInfo.getId());
                DistanceVo distanceVo = new DistanceVo();
                distanceVo.setDeviceId(vehicleInfo.getVinNumber());
                if(startInfo == null || endInfo == null ){
                    distanceVo.setDistance("0.0");
                }else {
                    distanceVo.setDistance(String.format("%.2f",endInfo.getDistance() - startInfo.getDistance()));
                }
                result.add(distanceVo);
            }
        }else{
            List<StatisticHistory> endInfoList = statisticHistoryDao.getCurrentDayList(statisticQueryParam.getStartTime());
            log.info("获取车辆数据：{}条，起始数据：{}条，结束数据结束：{}条",vehicleList.size(), startInfoList.size(),endInfoList.size());
            for (VehicleInfo vehicleInfo : vehicleList) {
                StatisticHistory startInfo = startInfoList.get(vehicleInfo.getId());
                StatisticHistory endInfo = endInfoList.get(vehicleInfo.getId());
                DistanceVo distanceVo = new DistanceVo();
                distanceVo.setDeviceId(vehicleInfo.getVinNumber());
                if(startInfo == null || endInfo == null ){
                    distanceVo.setDistance("0.0");
                }else {
                    distanceVo.setDistance(String.format("%.2f",endInfo.getDistance() - startInfo.getDistance()));
                }
                result.add(distanceVo);
            }
        }
        return result;
    }

    @Override
    public List getFaultRate(StatisticQueryParam statisticQueryParam) {
        //createQueryParam(statisticQueryParam);
        List<VehicleInfo> vehicleList = vehicleInfoDao.selectVehicleByEnableState(CterminalConst.EnableState.ABLE);
        List<StatisticHistory> faultRatioList = statisticHistoryDao.getFaultRatioList(statisticQueryParam);
        List<FaultRateVo> result = new ArrayList();
        for (VehicleInfo vahicleInfo : vehicleList) {
            FaultRateVo faultRateVo = new FaultRateVo();
            faultRateVo.setDeviceId(vahicleInfo.getVinNumber());
            StatisticHistory statisticHistory = faultRatioList.get(vahicleInfo.getId());
            if(statisticHistory != null){
                faultRateVo.setFaultRate(String.format("%.2f",statisticHistory.getFaultRatio()));
            }else{
                faultRateVo.setFaultRate("0.0");
            }
            result.add(faultRateVo);
        }
        return result;
    }


}
